package com.jettech.oa.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Type;
/**
 * 公文
 * @author Administrator
 *
 */
@Entity
@Table(name="jettech_document")
public class Document implements Serializable {
	private static final long serialVersionUID = -3598325816430513021L;
	private Long id;//主键
	private User user;//创建者
	private WorkFlow workFlow;//所属流程实例
	//private Map<String,DocumentProperty> properties;
	private List<DocumentProperty> properties;//文档的自定义表单集合
	private byte[] content;//附件文件
	private String name;//文档名称
	private String currentStateName;//公文所处流程的当前状态
	private String status;//状态
	private String desc;//描述
	private Date createdTime;//创建事件
	private Date updatedTime;//更新事件
	
	private long processInstanceId;//流程实例的主键
	
	public Document(){
		
	}
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="document_id")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="work_flow_id")
	public WorkFlow getWorkFlow() {
		return workFlow;
	}
	public void setWorkFlow(WorkFlow workFlow) {
		this.workFlow = workFlow;
	}
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="user_id")
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@OneToMany(targetEntity=DocumentProperty.class,fetch=FetchType.LAZY,mappedBy="document")
	public List<DocumentProperty> getProperties() {
		return properties;
	}
	public void setProperties(List<DocumentProperty> properties) {
		this.properties = properties;
	}
	@Type(type="binary")
	@Column(name="content_file",length=9999999)
	public byte[] getContent() {
		return content;
	}
	public void setContent(byte[] content) {
		this.content = content;
	}
	/**
	@CollectionOfElements
	@JoinTable(name="jettech_document_properties",
			joinColumns = @JoinColumn(name="document_id")
    )
    @MapKey(columns=@Column(name="document_property_name"))
	public Map<String, DocumentProperty> getProperties() {
		return properties;
	}
	public void setProperties(Map<String, DocumentProperty> properties) {
		this.properties = properties;
	}
	public Object getProperty(String name){
		try{
			return PropertyUtils.getProperty(this,name);
		}catch(Exception e){
			if(properties!=null){
				DocumentProperty dp = (DocumentProperty)properties.get(name);
				return dp.value();
			}else{
				throw new RuntimeException("没有找到相关的属性[name="+name+"]");
			}
		}
	}
	*/
	public Object getProperty(String name){
		if(properties!=null && properties.size()>0){
			DocumentProperty dpTemp = null;
			for(DocumentProperty dp:properties){
				if(dp.getName()!=null && dp.getName().equals(name)){
					dpTemp = dp;
					break;
				}
			}
			if(dpTemp!=null){
				return dpTemp.value();
			}else{
				throw new RuntimeException("没有找到相关的属性[name="+name+"]");
			}
		}else{
			throw new RuntimeException("没有找到相关的属性[name="+name+"]");
		}
    }
	@Column(name="document_name")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(name="current_state_name")
	public String getCurrentStateName() {
		return currentStateName;
	}
	public void setCurrentStateName(String currentStateName) {
		this.currentStateName = currentStateName;
	}
	@Column(name="process_instance_id")
	public long getProcessInstanceId() {
		return processInstanceId;
	}
	public void setProcessInstanceId(long processInstanceId) {
		this.processInstanceId = processInstanceId;
	}
	@Column(name="document_status",length=8)
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	@Column(name="document_desc")
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	@Column(name="created_time",updatable=false)
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreatedTime() {
		return createdTime;
	}
	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}
	@Column(name="updateed_time")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getUpdatedTime() {
		return updatedTime;
	}
	public void setUpdatedTime(Date updatedTime) {
		this.updatedTime = updatedTime;
	}
	public boolean equals(Object other){
		return EqualsBuilder.reflectionEquals(this, other);
	}
	public int hashCode(){
		return HashCodeBuilder.reflectionHashCode(this);
	}
	public String toString(){
		return ToStringBuilder.reflectionToString(this);
	}
}
